﻿using System;
using System.Data;
using System.Collections.Generic;
using SF.Common;
using SF.Model;
using System.Text;
namespace SF.BLL
{
	/// <summary>
	/// 订单信息
	/// </summary>
	public partial class sf_goods_order
	{
        private readonly SF.DAL.sf_goods_order dal = new SF.DAL.sf_goods_order();
        private readonly SF.BLL.sf_user_info userBll = new SF.BLL.sf_user_info();
		public sf_goods_order()
		{}
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long id)
        {
            return dal.Exists(id);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string where)
        {
            return dal.Exists(where);
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(SF.Model.sf_goods_order model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SF.Model.sf_goods_order model)
		{
			return dal.Update(model);
		}

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long id)
        {
            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string where)
        {
            return dal.Delete(where);
        }
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			return dal.DeleteList(idlist );
		}

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_goods_order GetModel(long id)
        {
            return dal.GetModel(id);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_goods_order GetModel(string where)
        {
            return dal.GetModel(where);
        }


		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<SF.Model.sf_goods_order> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<SF.Model.sf_goods_order> DataTableToList(DataTable dt)
		{
			List<SF.Model.sf_goods_order> modelList = new List<SF.Model.sf_goods_order>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				SF.Model.sf_goods_order model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return dal.GetList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }



		#endregion  BasicMethod
		#region  ExtensionMethod

        /// <summary>
        /// 计算某个字段的总和值
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public DataSet GetSumField(string field, string where)
        {
            return dal.GetSumField(field, where);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageIndex">查询条件</param>
        /// <param name="filedOrder">排序字段，多个则用“,”隔开</param>
        /// <param name="recordCount">记录总数</param>
        /// <param name="isOut">仅用于前台 订单列表 展示</param>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount, string isOur)
        {
            return dal.GetList(pageSize, pageIndex, strWhere, filedOrder, out  recordCount, isOur);
        }


        /// <summary>
        /// 修改打印 运单 数
        /// </summary>
        public bool UpdateExpress(long id)
        {
            int exr = Convert.ToInt32(GetModel(id).isExpress) + 1;
            return dal.UpdateField(id, "isExpress=" + exr);
        }


        /// <summary>
        /// 获取 参数 用户 的客户订单
        /// </summary>
        /// <param name="inviteKey">邀请码</param>
        /// <param name="level">客户级数</param>
        /// <returns></returns>
        public DataTable getLevelOrders(string inviteKey, int level,string where)
        {
            DataTable ds = dal.GetList("1!=1").Tables[0];
            DataTable dt = new DataTable();
            //判断级别 获取 参数级别的 客户
            if (level == 1)
            {
                dt = userBll.GetList(" fatherKey='" + inviteKey + "' and "+where).Tables[0];
            }
            else if (level == 2)
            {
                dt = userBll.getTwoUsers(inviteKey, where);
            }
            else if (level == 3)
            {
                dt = userBll.getThreeUsers(inviteKey, where);
            }
            else if (level == 4)
            {
                dt = userBll.GetList(" fatherKey='" + inviteKey + "' and "+where).Tables[0];
                foreach (DataRow row in dt.Rows)
                {
                    DataTable dd = dal.GetList("userNum='" + row["userNum"] + "' and isPay>1 and (payTime<'2015-11-07 00:00:00' or payTime>'2015-11-17 23:59:59') and " + where).Tables[0];
                    foreach (DataRow r in dd.Rows)
                    {
                        DataRow ro = ds.NewRow();
                        ro.ItemArray = r.ItemArray;
                        ds.Rows.Add(ro);
                    }
                }
                dt = userBll.getTwoUsers(inviteKey, where);
                foreach (DataRow row in dt.Rows)
                {
                    DataTable dd = dal.GetList("userNum='" + row["userNum"] + "' and isPay>1 and (payTime<'2015-11-07 00:00:00' or payTime>'2015-11-17 23:59:59') and " + where).Tables[0];
                    foreach (DataRow r in dd.Rows)
                    {
                        DataRow ro = ds.NewRow();
                        ro.ItemArray = r.ItemArray;
                        ds.Rows.Add(ro);
                    }
                }
                dt = userBll.getThreeUsers(inviteKey, where);

            }
            foreach (DataRow row in dt.Rows)
            {
                DataTable dd = dal.GetList("userNum='" + row["userNum"] + "' and isPay>1 and (payTime<'2015-11-07 00:00:00' or payTime>'2015-11-17 23:59:59') and " + where).Tables[0];
                foreach (DataRow r in dd.Rows)
                {
                    DataRow ro = ds.NewRow();
                    ro.ItemArray = r.ItemArray;
                    ds.Rows.Add(ro);
                }
            }
            return ds;
        }


        /// <summary>
        /// 获得财务结算的订单数据列表
        /// </summary>
        public DataSet GetFinanceOrderList(string strWhere)
        {
            return dal.GetFinanceOrderList(strWhere);
        }
        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(string strValue, string where)
        {
            return dal.UpdateField(strValue, where);
        }
		#endregion  ExtensionMethod
	}
}

